<?php
declare(strict_types=1);

namespace Zlf\Unit;

class Str
{

    /**
     * 字符串分割,过滤空数据
     * @param string $separator
     * @param string $string
     * @param bool $is_empty
     * @return array
     */
    public static function explode(string $separator, string $string, bool $is_empty = false): array
    {
        $data = explode($separator, $string);
        if ($is_empty) return $data;
        $newsData = [];
        foreach ($data as $item) {
            if (strlen($item) > 0) {
                $newsData[] = $item;
            }
        }
        return $newsData;
    }


    /**
     * 下划线转驼峰
     * @param string $string
     * @param string $symbol
     * @return string
     * @author 竹林风@875384189 2021/9/6 11:55
     */
    public static function doubleToHump(string $string, string $symbol = '_'): string
    {
        $data = self::explode($symbol, $string);
        $newName = '';
        foreach ($data as $index => $name) {
            if ($index) {
                $newName .= ucfirst($name);
            } else {
                $newName .= $name;
            }
        }
        return $newName;
    }


    /**
     * 不定参数输入生存MD5
     * @param ...$param
     */
    public static function md5(...$param): string
    {
        return md5(http_build_query(['param' => $param]));
    }


    /**
     * 生成16位MD5
     * @author 竹林风@875384189 2021/6/24 13:52
     */
    public static function md5_16(...$param): string
    {
        $md5 = self::md5(...$param);
        return substr($md5, 8, 16);
    }
}